import 'package:flutter/material.dart';
import 'package:flutter_application_1/components/choose_bottom_sheet/choose_bottom_sheet.dart';
import 'package:flutter_application_1/components/city_list_selector/city_list_selector.dart';
import 'package:flutter_application_1/components/flutter_jd_address_selector/flutter_jd_address_selector.dart';
import 'package:flutter_application_1/components/hide_scroll_shadow/hide_scroll_shadow.dart';
import 'package:flutter_application_1/components/loading_tip/loading_tip.dart';
import 'package:flutter_application_1/components/pay_password_dialog/pay_password_dialog.dart';
import 'package:fluttertoast/fluttertoast.dart';

class CommonDialogTest extends StatefulWidget {
  @override
  _CommonDialogTestState createState() => _CommonDialogTestState();
}

class _CommonDialogTestState extends State<CommonDialogTest> {
  _showSnackBar(String val) {
    if (val.isNotEmpty) {
      ScaffoldMessenger.of(context).hideCurrentSnackBar();
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(val),
          // action: SnackBarAction(
          //   label: '隐藏',
          //   textColor: Colors.white,
          //   onPressed: () {
          //     // ScaffoldMessenger.of(context).hideCurrentSnackBar();
          //   },
          // ),
        ),
      );
    }
  }

  _simpleDialogOptionBuilder({
    required IconData icon,
    required Color color,
    required String text,
    String? value,
  }) {
    return SimpleDialogOption(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(
            icon,
            size: 36,
            color: color,
          ),
          SizedBox(width: 16),
          Flexible(child: Text(text)),
        ],
      ),
      onPressed: () {
        Navigator.pop<String?>(context, value ?? text);
      },
    );
  }

  PersistentBottomSheetController? _bottomSheetController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('常用弹框'),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            children: [
              ElevatedButton(
                child: Text('提示'),
                onPressed: () {
                  showDialog<bool>(
                    context: context,
                    // 是否可以点击蒙层关闭
                    // barrierDismissible: false,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text('提示'),
                        content: Text(
                          '确定吗？',
                          textAlign: TextAlign.center,
                        ),
                        actions: [
                          TextButton(
                            child: Text('取消'),
                            onPressed: () => Navigator.pop(context, false),
                          ),
                          TextButton(
                            child: Text('确定'),
                            onPressed: () => Navigator.pop(context, true),
                          ),
                        ],
                      );
                    },
                  ).then((value) {
                    if (value == null) {
                      _showSnackBar('返回或者点击蒙层');
                    } else if (value) {
                      _showSnackBar('点击确定');
                    } else {
                      _showSnackBar('点击取消');
                    }
                  });
                },
              ),
              TextButton(
                child: Text('简洁'),
                onPressed: () {
                  showDialog<String?>(
                    context: context,
                    builder: (BuildContext context) {
                      return SimpleDialog(
                        title: Text('请选择'),
                        children: [
                          Container(
                            height: 180,
                            child: SingleChildScrollView(
                              physics: BouncingScrollPhysics(),
                              child: Column(
                                children: [
                                  _simpleDialogOptionBuilder(
                                    icon: Icons.account_circle,
                                    color: Theme.of(context).primaryColor,
                                    text: 'alcyh666',
                                  ),
                                  _simpleDialogOptionBuilder(
                                    icon: Icons.account_circle,
                                    color: Theme.of(context).primaryColor,
                                    text: 'fanzhiliang777',
                                  ),
                                  _simpleDialogOptionBuilder(
                                    icon: Icons.account_circle,
                                    color: Theme.of(context).primaryColor,
                                    text: 'hahaha888',
                                  ),
                                  _simpleDialogOptionBuilder(
                                    icon: Icons.account_circle,
                                    color: Theme.of(context).primaryColor,
                                    text: 'woshidsb999',
                                  ),
                                ],
                              ),
                            ),
                          ),
                          _simpleDialogOptionBuilder(
                            icon: Icons.add_circle,
                            color: Theme.of(context).disabledColor,
                            text: '添加账号',
                            value: 'add',
                          ),
                        ],
                      );
                    },
                  ).then((value) {
                    if (value == null) {
                      _showSnackBar('返回或者点击蒙层');
                    } else if (value == 'add') {
                      _showSnackBar('点击添加');
                    } else {
                      _showSnackBar('点击：$value');
                    }
                  });
                },
              ),
              Builder(
                builder: (BuildContext context) {
                  return ElevatedButton(
                    child: Text('可穿透底部弹框'),
                    onPressed: () {
                      _bottomSheetController = showBottomSheet(
                        context: context,
                        builder: (BuildContext context) {
                          return Container(
                            color: Colors.grey,
                            width: double.infinity,
                            height: 200,
                          );
                        },
                      );

                      _bottomSheetController?.closed.whenComplete(() {
                        setState(() {
                          _bottomSheetController = null;
                        });
                      });
                    },
                  );
                },
              ),
              TextButton(
                child: Text('关闭可穿透底部弹框'),
                onPressed: () {
                  if (_bottomSheetController != null) {
                    _bottomSheetController?.close();
                  }
                },
              ),
              ElevatedButton(
                child: Text('蒙层底部弹框'),
                onPressed: () {
                  showModalBottomSheet(
                    context: context,
                    isScrollControlled: true,
                    backgroundColor: Colors.transparent,
                    builder: (BuildContext context) {
                      return Container(
                        alignment: Alignment.bottomCenter,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.end,
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: [
                            Expanded(
                              flex: 1,
                              child: GestureDetector(
                                onTap: () => Navigator.pop(context),
                              ),
                            ),
                            Container(
                              height: 200,
                              color: Colors.white,
                              child: ListView(
                                children: List.generate(
                                  10,
                                  (index) => ListTile(
                                    title: Text('item$index'),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      );
                    },
                  );
                },
              ),
              TextButton(
                child: Text('密码弹框'),
                onPressed: () {
                  showModalBottomSheet<String>(
                    context: context,
                    isScrollControlled: true,
                    backgroundColor: Colors.transparent,
                    builder: (BuildContext context) {
                      return PayPasswordDialog();
                    },
                  ).then((value) {
                    if (value != null && value.isNotEmpty) {
                      Fluttertoast.showToast(msg: value);
                    }
                  });
                },
              ),
              ElevatedButton(
                child: Text('日期选择'),
                onPressed: () {
                  DateTime now = DateTime.now();
                  showDatePicker(
                    context: context,
                    initialDate: now,
                    firstDate: now,
                    lastDate: now.add(
                      Duration(
                        days: 31,
                      ),
                    ),
                  ).then((value) => print(value));
                },
              ),
              TextButton(
                child: Text('时间选择'),
                onPressed: () {
                  TimeOfDay now = TimeOfDay.now();
                  showTimePicker(
                    context: context,
                    initialTime: now,
                  ).then((value) {
                    DateTime nowtTime = DateTime.now();
                    DateTime result = DateTime(nowtTime.year, nowtTime.month,
                        nowtTime.day, now.hour, now.minute);
                    print(result);
                  });
                },
              ),
              ElevatedButton(
                child: Text('日期范围选择'),
                onPressed: () {
                  DateTime now = DateTime.now();
                  showDateRangePicker(
                    context: context,
                    firstDate: now,
                    lastDate: now.add(
                      Duration(
                        days: 31,
                      ),
                    ),
                  ).then((value) => print(value));
                },
              ),
              TextButton(
                child: Text('地区选择'),
                onPressed: () {
                  showModalBottomSheet(
                    context: context,
                    builder: (BuildContext context) {
                      return JDAddressDialog(
                        province: '广东省',
                        city: '广州市',
                        county: '南沙区',
                        unselectedColor: Colors.black,
                        selectedColor: Color.fromRGBO(26, 188, 156, 1),
                        onSelected:
                            (String province, String city, String county) {
                          print('$province $city $county');
                        },
                      );
                    },
                  );
                },
              ),
              ElevatedButton(
                child: Text(
                    '自定义loading (使用 Overlay) (点击${LoadingTip.loading ? '关闭' : '开启'})'),
                onPressed: () {
                  if (LoadingTip.loading) {
                    LoadingTip.remove();
                  } else {
                    LoadingTip.show(context: context);
                  }
                  setState(() {});
                },
              ),
              TextButton(
                child: Text('自定义loading (使用 showGeneralDialog)'),
                onPressed: () {
                  LoadingTip.show(
                    context: context,
                    isMask: true,
                    ableBack: true,
                  );
                },
              ),
              ElevatedButton(
                child: Text('城市列表选择'),
                onPressed: () {
                  showCityListSelector<String>(
                    context: context,
                  ).then((city) {
                    print(city);
                  });
                },
              ),
              TextButton(
                child: Text('自定义底部弹框'),
                onPressed: () {
                  showChooseBottomSheet<int?>(
                    context: context,
                    sheets: [
                      ChooseButton(
                        text: '1',
                        onTap: () {
                          Navigator.pop(context, 1);
                        },
                      ),
                      ChooseButton(
                        text: '2',
                        onTap: () {
                          Navigator.pop(context, 2);
                        },
                      ),
                    ],
                  ).then((value) => print(value));
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
